﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MP3Ganizer.DAL
{
    public class Query
    {
        private MP3GanizerEntities _db;
        private static Query _instance;

        public static Query Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Query();
                return _instance;
            }
        }

        public Query()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MP3GanizerEntities"].ConnectionString;
            _db = new MP3GanizerEntities(connectionString);
        }

        public void TryInsertArtiste(string artiste)
        {
            IEnumerable<Artiste> artistes = from a in _db.Artiste
                                            where a.ArtistName == artiste
                                            select a;
            if (!artistes.ToList<Artiste>().Exists(x =>
            {
                return x.ArtistName == artiste;
            }))
            {
                Artiste a = Artiste.CreateArtiste(Guid.NewGuid(), artiste);
                _db.AddToArtiste(a);
                _db.SaveChanges();
            }
        }

        public void TryInsertAlbum(string album, string artiste)
        {
             IEnumerable<Artiste> artistes = from a in _db.Artiste
                                            where a.ArtistName == artiste
                                            select a;

            Artiste artisteObj = new Artiste();
            IEnumerable<Album> albums;
            if (artistes.ToList<Artiste>().Exists(x=> 
                {
                    return x.ArtistName == artiste;
                }))
            {
                artisteObj = artistes.FirstOrDefault();

                albums = from a in _db.Album
                         where a.AlbumName == album && (a.ArtistId == artisteObj.ArtistId)
                         select a;
            }
            else
            {
                artisteObj = null;
                albums = from a in _db.Album
                         where a.AlbumName == album
                         select a;
            }
            if (!albums.ToList<Album>().Exists(x=> 
                {
                    return x.AlbumName == album;
                }))
            {
                Album a = Album.CreateAlbum(Guid.NewGuid(), album);
                if (artisteObj != null)
                    a.ArtistId = artisteObj.ArtistId;
                _db.AddToAlbum(a);
                _db.SaveChanges();
            }
        }

        public List<string> GetAllArtiste()
        {
            IEnumerable<Artiste> artistes = from a in _db.Artiste
                                            select a;
            List<string> artistStrings = new List<string>();
            artistes.ToList<Artiste>().ForEach(a => artistStrings.Add(a.ArtistName));
            return artistStrings;
        }

        public List<string> GetAllAlbum()
        {
            IEnumerable<Album> albums = from a in _db.Album
                                        select a;
            List<string> albumStrings = new List<string>();
            albums.ToList<Album>().ForEach(a => albumStrings.Add(a.AlbumName));
            return albumStrings;
        }
    }
}
